window.onload = function(){
    var host = 'http://fire.tomobo.com/api/';

    $.ajax({
        url: host + '?m=system.FrontPage.getPosition',
        type: "post",
        dataType: "json",
        success: function (msg) {
            list = msg;
            var total = msg.base.total;
            var yan = msg.base.yan;
            var wen = msg.base.wen;
            var qi = msg.base.qi;
            var alert = msg.base.alert;
            var online = msg.base.online;
            var a = '';
            var b = '';
            var y = '';
            var w = '';
            var q = '';
            var rate = ((online / total) * 100).toFixed(2);

            var info = msg.info;
            var cluster, markers = [];
            var map = new AMap.Map("container", {
                resizeEnable: true,
                center: [114, 23],
                zoom: 9
            });
            for (var i = 0; i < info.length; i += 1) {
                var aa = info[i].position.split(',')[0];
                var bb = info[i].position.split(',')[1];
                var type = info[i].type;
                if (type == 11) { //烟
                    icon = 'yanwu1.png';
                }
                else if (type == 12) {//温
                    icon = 'wendu1.png';
                }
                else {//气
                    icon = 'ranqi1.png';
                }
                var startIcon = new AMap.Icon({
                    // 图标尺寸
                    size: new AMap.Size(27.56, 35),
                    // 图标的取图地址
                    image: icon,
                    // 图标所用图片大小
                    imageSize: new AMap.Size(27.56, 35),
                });
                var marker = new AMap.Marker({
                    position: [aa, bb],
                    icon: startIcon,
                    offset: new AMap.Pixel(-15, -15)
                });
                markers.push(marker);
                marker.id = info[i].id;
                marker.codeserial = info[i].codeserial;
                marker.device_address = info[i].device_address;
                marker.on('click', markerClick);
                AMap.event.addListener(marker, 'click', markerClick)
            }
            var sts = [
                {
                    url: "m1.png",
                    size: new AMap.Size(53, 53),
                    textColor: '#f5f7fa'
                }];
            cluster = new AMap.MarkerClusterer(map, markers, { minClusterSize: 2, gridSize: 80, styles: sts });
        }
    });
    window.vm = new Vue({
        el: '#alert_root',
        template: '#alarm-window-tpl',
        data: {
            alertInfo: null,
            alertinterval: null,
            audiointerval: null,
            isShowWindow: false,
            isShowContent: true,
            formValue: [],
            currentIndex: 0,
            isAudioPlay:false
        },
        mounted: function () {
            this.startInterval();
        },
        computed: {
            alertList: function () {
                var list = this.alertInfo.alert_list || [];
                for (var i = 0; i < list.length; i++) {
                    var item = list[i];
                    this.formValue[i] = {
                        id: item.id,   //警报id
                        alarm_type: '1',//警报类型
                        disposal_platform: 1, //解除平台
                        'remark': ''
                    };
                }
                return list;
            },
            canNext: function () {
                return !((this.currentIndex + 1) >= this.alertList.length);
            },
            canPrev: function () {
                return !((this.currentIndex + 1) <= 1);
            }
        },
        methods: {
            deal: function () {
                var $that = this;
                var payload = {
                    codeserial: this.alertInfo.codeserial,
                    data: [],
                }
                for (var i = 0; i < this.formValue.length; i++) {
                    payload.data[i] = this.formValue[i];
                }
                //处置时暂停轮询
                this.stopInterval();
                window.parent.dealee(payload, function (res) {
                    
                    if(res.code == 'cancel'){//cancel是取消操作
                        //Todo
                    }else if (res.code != '0000') {//处理成功
                        //处理成功后alertInfo设置为null;
                        $that.alertInfo = null;
                        $that.isShowWindow = false;
                        $that.clearAudioInterval();
                        window.updateAlertHead();
                    } 
                    else { //处理失败
                        console.error('请求异常，请刷新重试');
                    }
                    $that.startInterval();
                });
            },
            getAlert: function () {
                var $that = this;
                $.ajax({
                    url: host + '?m=system.FrontPage.newAlertInfo',
                    type: "post",
                    dataType: "json",
                    success: function (msg) {
                        if (msg.code == 1) {
                            // if (msg.info instanceof Array && msg.info.length) {
                            if (msg.info instanceof Array && msg.info.length) {
                                //有新报警时应及时更新，alert_head
                                if(window.tops_base.alert != msg.info.length){
                                    window.updateAlertHead();
                                }
                                //当前警报为null时，缓存第一条警报信息
                                if(!$that.alertInfo){
                                    //每次取警报的第一台设备
                                    $that.alertInfo = msg.info[0];
                                }
                                if ($that.isShowWindow === false) {
                                    $that.isShowWindow = true;
                                    $that.audiointerval = setInterval($that.clock, 6000);
                                }
                            }
                        }
                        else {
                            $that.isShowWindow = false;
                            $that.clearAudioInterval();
                        }
                    }
                });
            },
            next: function () {
                this.canNext && this.currentIndex++;
            },
            prev: function () {
                this.canPrev && this.currentIndex--;
            },
            //开始查询报警轮询
            startInterval: function () {
                this.alertinterval = setInterval(this.getAlert, 3000);
            },
            //暂停查询报警轮询
            stopInterval: function () {
                clearInterval(this.alertinterval);
            },
            clearAudioInterval: function () {
                    this.isAudioPlay = false;
                    clearInterval(this.audiointerval);
            },
            triggerAudio: function () {
                var audio = document.getElementById('music1');
                if (audio.paused) {
                    audio.play();
                    this.isAudioPlay = true;
                }
                else {
                    audio.pause();// 这个就是暂停
                    this.audiointerval && this.clearAudioInterval();
                }
            },
            clock: function () {
                var audio = document.getElementById('music1');
                this.isAudioPlay = true;
                audio.play();
            },
            triggerAlertContent: function () {
                this.isShowContent = !this.isShowContent;
            },

            submitObject: function () {
                var obj = {
                    codeserial: this.alertInfo.codeserial,
                    data: []
                };
                for (var i = 0; i < this.alertInfo.alert_list; i++) {
                    obj.data[i] = {
                        id: '1',   //警报id
                        alarm_type: '1',//警报类型
                        disposal_platform: 1, //解除平台
                        'remark': '解除说明'
                    }
                }
                return {
                    data: [

                    ]
                }
            }
        }
    })

}

